from datetime import datetime
import uuid
from sqlalchemy import Column, types, UniqueConstraint
from sqlalchemy.dialects.mssql import TINYINT

from .base import BaseModel, ModelMixin


class UserMessage(BaseModel, ModelMixin):
    """ 用户聊天表 """

    __tablename__ = 'user_messages'

    TYPE_LAWER = 1  # 律师
    TYPE_PROPERTY = 2  # 物业
    TYPE_INDUSTRY = 3  # 业委会
    TYPE_GROUP = 4  # 群聊

    id = Column(types.BigInteger, primary_key=True)
    user_id = Column(types.String(50), nullable=False)
    from_type = Column(TINYINT, nullable=False, default='')
    from_name = Column(types.String(50), nullable=False, default='')
    from_token = Column(types.String(50), nullable=False, default='')

    to_type = Column(TINYINT, nullable=False, default='')
    to_user_id = Column(types.String(50), nullable=False)
    to_name = Column(types.String(50), nullable=False, default='')
    to_token = Column(types.String(50), nullable=False, default='')

    has_read = Column(types.Boolean, nullable=False, default=False)
    content = Column(types.String(100), nullable=False, default='')
    content_type = Column(types.BigInteger, nullable=False, default=0)
    ticket = Column(types.JSON, nullable=False)
    created_at = Column(types.DateTime, nullable=False, default=datetime.now)


class A(object):
    @staticmethod
    def set_token():
        return str(uuid.uuid4())


class GroupUser(BaseModel, ModelMixin, A):
    """ 群表 """

    __tablename__ = 'group_user'

    TYPE_LAWER = 1  # 律师
    TYPE_PROPERTY = 2  # 物业
    TYPE_INDUSTRY = 3  # 业委会

    id = Column(types.BigInteger, primary_key=True)
    # 群名称
    group_name = Column(types.String(50), nullable=False)
    # 群二维码
    group_url = Column(types.String(50), nullable=False, default='')
    # 群头像
    group_avatar = Column(types.String(255), nullable=False,
                          default="https://ai-community.oss-cn-beijing.aliyuncs.com/auth/群.jpg")
    # # 公告
    # group_public =
    # 创建时间
    created_time = Column(types.DateTime, nullable=False, default=datetime.now)
    # 创建用户id
    created_user_id = Column(types.String(50), nullable=False)
    # 创建用户类型
    created_user_type = Column(TINYINT, nullable=False)
    # 创建用户token
    created_user_token = Column(types.String(50), nullable=False)
    # # 群成员列表
    token = Column(types.String(50), nullable=False, default=A.set_token)

    # group_suers =


class GroupUserMid(BaseModel, ModelMixin):
    """ 群/成员关系表 """

    __tablename__ = 'group_user_mid'

    TYPE_LAWER = 1  # 律师
    TYPE_PROPERTY = 2  # 物业
    TYPE_INDUSTRY = 3  # 业委会

    id = Column(types.BigInteger, primary_key=True)
    # 群id
    group_id = Column(types.BigInteger)
    # 用户id
    user_id = Column(types.String(50), nullable=False)
    # 群昵称
    user_name = Column(types.String(50), nullable=False)
    # 用户类型
    user_type = Column(TINYINT, nullable=False, default='')
    # 用户token
    token = Column(types.String(50), nullable=False)
    # 进群时间
    join_time = Column(types.DateTime, nullable=False, default=datetime.now)
    # 最后阅读id
    last_read_id = Column(types.BigInteger, nullable=True)

    # class GroupPublic(BaseModel, ModelMixin):
    #     id = Column(types.BigInteger, primary_key=True)
    #     # 公告名称
    #     name = Column(types.String(50), nullable=False)
